<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <el-form-item prop="name" label="名称">
                        <el-input
                            v-model="baTable.form.items!.name"
                            type="string"
                            :placeholder="t('Please input field', { field: t('名称') })"
                        ></el-input>
                    </el-form-item>

                    <el-form-item prop="sort" label="排序">
                        <el-input
                            v-model="baTable.form.items!.sort"
                            type="number"
                            :placeholder="t('Please input field', { field: t('排序') })"
                        ></el-input>
                    </el-form-item>

                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { buildValidatorData } from '/@/utils/validate'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    coupons_title: [buildValidatorData({ name: 'required', title: t('coupons.coupons.Coupons Name') })],
    amount: [
        buildValidatorData({ name: 'required', title: t('coupons.coupons.amount') }),
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val || parseFloat(val) == 0) {
                    return callback(new Error(t('Please enter the correct field', { field: t('coupons.coupons.amount') })))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    min_order: [
        buildValidatorData({ name: 'required', title: t('coupons.coupons.min_order') }),
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val || parseFloat(val) == 0) {
                    return callback(new Error(t('Please enter the correct field', { field: t('coupons.coupons.min_order') })))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    min_reduce: [
        buildValidatorData({ name: 'required', title: t('coupons.coupons.min_reduce') }),
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (!val || parseFloat(val) == 0) {
                    return callback(new Error(t('Please enter the correct field', { field: t('coupons.coupons.min_reduce') })))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
    start_date: [
        buildValidatorData({
            name: 'required',
            message: t('Please select field', { field: t('coupons.coupons.start_date') }),
        }),
    ],
    end_date: [
        buildValidatorData({
            name: 'required',
            message: t('Please select field', { field: t('coupons.coupons.end_date') }),
        }),
    ],
})
</script>

<style scoped lang="scss"></style>
